package com.sinog.biz.service.business.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinog.biz.mapper.havexml.AreaCodeFullMapper;
import com.sinog.biz.mapper.havexml.SocialOrganizeInfoMapper;
import com.sinog.biz.mapper.havexml.SystemCodeMapper;
import com.sinog.biz.service.business.SocialOrganizeInfoService;
import com.sinog.biz.util.IdGen;
import com.sinog.core.domain.SocialOrganizeInfo;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.util.PageReturnUtil;
import com.sinog.core.util.PhoneUtil;
import com.sinog.core.util.StrUtil;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @desc 安帮工作资源管理 - 安帮社会组织信息管理 - 安帮社会组织信息管理 - SocialOrganizeInfoServiceImpl
 * @author dangyf
 * @date 2020年2月14日 20点48分
 */
@Service
public class SocialOrganizeInfoServiceImpl extends ServiceImpl<SocialOrganizeInfoMapper,SocialOrganizeInfo> implements SocialOrganizeInfoService {

    @Resource
    private SocialOrganizeInfoMapper socialOrganizeInfoMapper;
    @Resource
    private SystemCodeMapper systemCodeMapper;
    @Resource
    private AreaCodeFullMapper areaCodeFullMapper;

    /**
     * 安帮工作资源管理 - 安帮社会组织信息管理 - 安帮社会组织信息管理 - 列表
     * @param searchKey searchKey
     * @param pageIndex pageIndex
     * @param pageSize pageSize
     * @param searchOrgType searchOrgType
     * @param departId departId
     * @return String
     */
    @Override
    public String getSocialOrgInfoList(String searchKey,Integer pageIndex,Integer pageSize,String searchOrgType,String departId) {
        searchKey = StrUtil.converseToNull(searchKey);
        searchOrgType = StrUtil.converseToNull(searchOrgType);
        Page<Map<String,String>> page = new Page<>(pageIndex + 1,pageSize);
        List<Map<String,String>> socialOrgInfoList = socialOrganizeInfoMapper.getSocialOrgInfoList(page,searchKey,searchOrgType,departId);
        for(Map<String,String> map : socialOrgInfoList) {
            if(map.get("lxfs") != null) {
                Map<String,String> telAndPhone = PhoneUtil.splitTelAndPhone(map.get("lxfs"));
                map.put("lxfs",telAndPhone.get("phone"));
                map.put("tel",telAndPhone.get("tel"));
            }
        }
        page = page.setRecords(socialOrgInfoList);
        return PageReturnUtil.pageReturn(page);
    }

    /**
     * 安帮工作资源管理 - 安帮社会组织信息管理 - 安帮社会组织信息管理 - （修改/新增）
     * @param data data
     * @param loginUserParam loginUserParam
     * @return Result
     */
    @Override
    public Result save(String data,Map<String,String> loginUserParam) {
        SocialOrganizeInfo socialOrganizeInfo = JsonUtil.toBean(data,SocialOrganizeInfo.class);
        //对只有电话/只有固话的进行|的去除
        String lxfs = socialOrganizeInfo.getLxfs();
        if (StringUtils.isNotBlank(lxfs)) {
            Character one = lxfs.charAt(0);
            Character last = lxfs.charAt(lxfs.length()-1);
            if ("|".equals(one.toString()) || "|".equals(last.toString())) {
                String pxTel1 = lxfs.replace("|","");
                socialOrganizeInfo.setLxfs(pxTel1);
            }
        }
        // 住址省、市、县、街道名称
        String provinceCode = socialOrganizeInfo.getProvince();
        String cityCode = socialOrganizeInfo.getCity();
        String countyCode = socialOrganizeInfo.getCounty();
        String villageCode = socialOrganizeInfo.getVillage();
        socialOrganizeInfo.setBffwCode(Constants.NumberStr.NUM_STR_FU_ONE.equals(socialOrganizeInfo.getBffwCode()) ? null : socialOrganizeInfo.getBffwCode());
        if (StringUtils.isNotBlank(provinceCode)){
            Map<String,String> areaNames = areaCodeFullMapper.getAreaNames(provinceCode,cityCode,countyCode,villageCode);
            if (areaNames == null){
                return Result.failure(ReturnCode.SAVE_FAILED);
            }
            socialOrganizeInfo.setProvinceName(areaNames.get("shengName"));
            socialOrganizeInfo.setCityName(areaNames.get("dishiName"));
            socialOrganizeInfo.setCountyName(areaNames.get("quxianName"));
            socialOrganizeInfo.setVillageName(areaNames.get("jiedaoName"));
            if (StringUtils.isBlank(cityCode)){
                socialOrganizeInfo.setCityName("");
            }
            if (StringUtils.isBlank(countyCode)){
                socialOrganizeInfo.setCountyName("");
            }
            if (StringUtils.isBlank(villageCode)){
                socialOrganizeInfo.setVillageName("");
            }
        } else {
            socialOrganizeInfo.setProvinceName("");
            socialOrganizeInfo.setCityName("");
            socialOrganizeInfo.setCountyName("");
            socialOrganizeInfo.setVillageName("");
        }
        boolean mark = null == socialOrganizeInfo.getId() || "".equals(socialOrganizeInfo.getId());
        if(mark) {
            socialOrganizeInfo.setId(IdGen.strid());
            socialOrganizeInfo.setClab(loginUserParam.get("departId"));
            socialOrganizeInfo.setClabName(loginUserParam.get("departName"));
            socialOrganizeInfo.setCreateUser(loginUserParam.get("loginUser"));
            Date date = new Date();
            socialOrganizeInfo.setCreateTime(date);
            socialOrganizeInfo.setUpdateUser(loginUserParam.get("loginUser"));
            socialOrganizeInfo.setUpdateTime(date);
            boolean insertResult = socialOrganizeInfo.insert();
            if(insertResult) {
                return Result.success("保存成功");
            }
        } else {
            socialOrganizeInfo.setUpdateUser(loginUserParam.get("loginUser"));
            socialOrganizeInfo.setUpdateTime(new Date());
            boolean updateResult = socialOrganizeInfo.updateById();
            if(updateResult) {
                return Result.success("保存成功");
            }
        }
        return Result.failure(ReturnCode.SAVE_FAILED);
    }

    /**
     * 安帮工作资源管理 - 安帮社会组织信息管理 - 安帮社会组织信息管理 - 删除
     * @param id id
     * @return Result
     */
    @Override
    public Result deleteOne(String id) {
        SocialOrganizeInfo socialOrganizeInfo = new SocialOrganizeInfo();
        socialOrganizeInfo.setId(id);
        boolean result = socialOrganizeInfo.deleteById(id);
        if(result) {
            return Result.success("删除成功！");
        }
        return Result.failure(ReturnCode.DELETE_FAILED);
    }

    /**
     * 安帮工作资源管理 - 安帮社会组织信息管理 - 安帮社会组织信息管理 - 导出
     * @param departId departId
     * @return List<Map < String,Object>>
     */
    @Override
    public List<Map<String,Object>> selectSocialOrgInfoForExport(String departId) {
        return socialOrganizeInfoMapper.selectSocialOrgInfoForExport(departId);
    }
}